package day20171003;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import java.io.FileWriter;
import java.io.IOException;

public class TestJsoup {
    public static void main(String[] args) throws IOException {
        Document d= Jsoup.connect("https://www.tripadvisor.cn/Attractions-g297462-Activities-Sichuan.html").get();
        Elements e= d.select("div.shelf_container");
        Elements e1= e.select("a[class=poiTitle]");
        Elements e2= e.select("div.item");
        System.out.println(e2.text());
        String path="/home/automan/Desktop/SichuanProvinceLight.txt";
        FileWriter fileWriter=null;
        int i=e1.size();
        for (int j=0;j<i;j++) {
            try {
                fileWriter = new FileWriter(path, true);
                fileWriter.write(e1.get(j).text());
                fileWriter.write(" : ");
                fileWriter.write(e2.get(3*(j+1)-1).text());
                fileWriter.write("\n");
                fileWriter.flush();
            } catch (IOException en) {
                en.printStackTrace();
            } finally {
                if (fileWriter != null) {
                    try {
                        fileWriter.close();
                    } catch (IOException ee) {
                        ee.printStackTrace();
                    }
                }

            }
        }
    }
}
